﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin.AboutGuild
{
	public partial class CouncilUpdate : BasePage
	{
		protected string CID { get; set; }
		protected string ConTitle { get; set; }
		/// <summary>
		/// 系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00010004";//理事会 默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, SystemCode))
			{
				AdminBasePage.RedirectWithMessage(AdminDefaultPage, "您没有访问当前页面的权限,请与系统管理员联系!");
				return;
			}
			string CinfoID = Request.QueryString["cid"];
			this.CID = CinfoID;
			this.HidCID.Value = CinfoID;
			if (!IsPostBack)
			{
				ShowListCon(CinfoID);
			}
		}

		protected void ShowListCon(string CinfoID)
		{
			int Cid = 0;
			if (Int32.TryParse(CinfoID, out Cid))
			{
				TB_CouncilInfo resultC = DAL.DALHelper.Instance.GetObject<TB_CouncilInfo>("Index", Cid);
				this.CouncilName.Text = "内容编辑：" + resultC.Name;
				this.ConTitle = "内容编辑：" + resultC.Name;
				this.CouncilTitle.Text = resultC.Name;
				this.ECouncilTitle.Text = resultC.EnName;
				if (resultC.StartTime.ToString("yyyy-MM-dd") == "0001-01-01")
				{
					this.StartDate.Text = "";
				}
				else
				{
					this.StartDate.Text = resultC.StartTime.ToString("yyyy-MM-dd");
				}
				if (Convert.ToDateTime(resultC.EndTime).ToString("yyyy-MM-dd") == "0001-01-01")
				{
					this.EndDate.Text = "";
				}
				else
				{
					this.EndDate.Text = Convert.ToDateTime(resultC.EndTime).ToString("yyyy-MM-dd");
				}

				this.Introduce.Text = resultC.Memo;
				this.EIntroduce.Text = resultC.EnMemo;
				// 会员单位
				StrHtml(Cid);
			}
			else
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('页面加载错误！请注销重新登入。');window.close();</script>");
				return;
			}

		}

		protected void StrHtml(int CID)
		{
			StringBuilder SHtml = new StringBuilder();

			// 会员关系
			Expression<Func<TB_CouncilRelation, bool>> f = (p => p.Index == CID);
			Expression<Func<TB_CouncilRelation, string>> order = (p => p.Type);
			var resultR = DAL.DALHelper.Instance.GetList<TB_CouncilRelation, string>(f, order, true);

			// 会员
			Expression<Func<TB_Member, bool>> f2 = (p => true);
			Expression<Func<TB_Member, int>> order2 = (p => p.ID);
			var resultRM = DAL.DALHelper.Instance.GetList<TB_Member, int>(f2, order2, true);

			// 字典
			// TODO 只区一级
			Expression<Func<TB_SystemMap, bool>> f3 = (p => p.Code.StartsWith("000100050") && p.Code.Length == 12);
			Expression<Func<TB_SystemMap, string>> order3 = (p => p.Code);
			var resultSM = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f3, order3, true);

			// 分类
			foreach (var lClass in resultSM)
			{
				// 检测是否有子类
				string MidCode = lClass.Code + "0";
				Expression<Func<TB_SystemMap, bool>> f4 = (p => p.Code.StartsWith(MidCode));
				Expression<Func<TB_SystemMap, string>> order4 = (p => p.Code);
				var resultSMC = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f4, order4, true);
				SHtml.Append("<div id='memdp_header'><span>");
				SHtml.Append(lClass.Name);
				SHtml.Append("</span></div>");
				if (resultSMC.Count == 0)
				{
					SHtml.Append("<div id='memdpBorder'><ul>");
					var CodeMember = resultR.Where(c => (c.Type == lClass.Code)).ToList();
					foreach (var LMeber in CodeMember)
					{
						SHtml.Append("<li>");
						var MemberName = resultRM.Where(c => c.ID == LMeber.MemberID).FirstOrDefault();
						SHtml.Append("<a href='MemberEdit.aspx?mid=" + LMeber.MemberID + "&cid=" + CID + "&st=00010005' target='_blank' >" + MemberName.Name + "</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='del_click(" + LMeber.ID + ")' >[删除]</a></li>");
					}
					SHtml.Append("</ul></div>");
					SHtml.Append("<div style='clear:both;'></div>");
				}
				else
				{
					SHtml.Append(" <div id='memdpBorder'>");
					foreach (var SecLClass in resultSMC)
					{
						SHtml.Append("<div id='memdpbox'><div id='memdptitle'>");
						SHtml.Append(SecLClass.CText);
						SHtml.Append("</div><div id='memdpBordertitle'><ul>");
						var SecCodeMember = resultR.Where(c => (c.Type == SecLClass.Code)).ToList();
						foreach (var LSMeber in SecCodeMember)
						{
							SHtml.Append("<li>");
							var MemberName = resultRM.Where(c => c.ID == LSMeber.MemberID).FirstOrDefault();
							SHtml.Append("<a href='MemberEdit.aspx?mid=" + LSMeber.MemberID + "&cid=" + CID + "&st=00010005' target='_blank' >" + MemberName.Name + "</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='del_click(" + LSMeber.ID + ")' >[删除]</a></li>");
						}
						SHtml.Append("</ul></div></div>");
					}
					SHtml.Append("</div>");
					SHtml.Append("<div style='clear:both;'></div>");
				}
			}

			this.main_rbox.InnerHtml = SHtml.ToString();

		}

		protected void SubBtn_Click(object sender, EventArgs e)
		{
			int CID = Convert.ToInt32(this.HidCID.Value);
			TB_CouncilInfo result = DAL.DALHelper.Instance.GetObject<TB_CouncilInfo>("Index", CID);

			if (result == null)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('页面加载错误！请注销重新登入后操作。');window.close();</script>");
				return;
			}
			result.Name = this.CouncilTitle.Text;
			result.EnName = this.ECouncilTitle.Text;
			if (string.IsNullOrWhiteSpace(this.StartDate.Text))
			{
				result.StartTime = Convert.ToDateTime(null);
			}
			else
			{
				result.StartTime = Convert.ToDateTime(this.StartDate.Text);
			}
			if (string.IsNullOrWhiteSpace(this.EndDate.Text))
			{
				result.EndTime = Convert.ToDateTime(null);
			}
			else
			{
				result.EndTime = Convert.ToDateTime(this.EndDate.Text);
			}

			result.Memo = this.Introduce.Text;
			result.EnMemo = this.EIntroduce.Text;

			DAL.DALHelper.Instance.Update<TB_CouncilInfo>(result);
			DAL.DALHelper.Instance.SaveChanges();

			RefreshNewsList();
		}

		private void RefreshNewsList()
		{
			string clientJs = "<script type=\"text/javascript\" language=\"javascript\">";
			clientJs += "if(window.opener && window.opener.RefreshNewsList) window.opener.RefreshNewsList();";
			clientJs += "window.close();</script>";
			Response.Write(clientJs);
			Response.End();
		}

		protected void Del_Click(object sender, EventArgs e)
		{
			int id = 0;
			if (Int32.TryParse(this.Hid_ID.Value, out id))
			{
				var result = DAL.DALHelper.Instance.GetObject<TB_CouncilRelation>("ID", id);
				if (result == null)
				{
					ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('页面加载错误！请注销重新登入后操作。');window.close();</script>");
					return;
				}
				else
				{
					DAL.DALHelper.Instance.Delete<TB_CouncilRelation>(result);
					DAL.DALHelper.Instance.SaveChanges();
					ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('删除成功！');location=location;</script>;");
					return;
				}
			}
		}

	}
}